package cn.youqu.happy.module.youqu.controller.admin.userAssess;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userAssess.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userAssess.UserAssessDO;
import cn.youqu.happy.module.youqu.convert.userAssess.UserAssessConvert;
import cn.youqu.happy.module.youqu.service.userAssess.UserAssessService;

@Api(tags = "管理后台 - 用户评价")
@RestController
@RequestMapping("/youqu/user-assess")
@Validated
public class UserAssessController {

    @Resource
    private UserAssessService userAssessService;

    @PostMapping("/create")
    @ApiOperation("创建用户评价")
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:create')")
    public CommonResult<Integer> createUserAssess(@Valid @RequestBody UserAssessCreateReqVO createReqVO) {
        return success(userAssessService.publishUserAssess(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户评价")
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:update')")
    public CommonResult<Boolean> updateUserAssess(@Valid @RequestBody UserAssessUpdateReqVO updateReqVO) {
        userAssessService.updateUserAssess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:delete')")
    public CommonResult<Boolean> deleteUserAssess(@RequestParam("id") Integer id) {
        userAssessService.deleteUserAssess(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:query')")
    public CommonResult<UserAssessRespVO> getUserAssess(@RequestParam("id") Integer id) {
        UserAssessDO userAssess = userAssessService.getUserAssess(id);
        return success(UserAssessConvert.INSTANCE.convert(userAssess));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户评价列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:query')")
    public CommonResult<List<UserAssessRespVO>> getUserAssessList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserAssessDO> list = userAssessService.getUserAssessList(ids);
        return success(UserAssessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户评价分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:query')")
    public CommonResult<PageResult<UserAssessRespVO>> getUserAssessPage(@Valid UserAssessPageReqVO pageVO) {
        PageResult<UserAssessDO> pageResult = userAssessService.getUserAssessPage(pageVO);
        return success(UserAssessConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户评价 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-assess:export')")
    @OperateLog(type = EXPORT)
    public void exportUserAssessExcel(@Valid UserAssessExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserAssessDO> list = userAssessService.getUserAssessList(exportReqVO);
        // 导出 Excel
        List<UserAssessExcelVO> datas = UserAssessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户评价.xls", "数据", UserAssessExcelVO.class, datas);
    }

}
